<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * @property mixed form_validation
 */
class Khoahoc extends CI_Controller {

    // num of records per page
    private $limit = 10;
    public $data;
    function __construct()
    {
        parent::__construct();
        $this->data['tinyMCE']=true;
        // load library
        $this->load->library(array('table','form_validation'));

        // load helper
        $this->load->helper('url');

        // load model
        $this->load->model(array('admin/Khoahoc_model','admin/nganh_model','admin/mon_model','user_model'));
        $this->data = array(
            'nganh' => $this->nganh_model->getNganh(),
            'mon'   => $this->mon_model->getMon()
        );
        $this->is_logged_in();
        $this->isAdmin();
        $this->isTeacher();

    }
    function is_logged_in()
    {
        $is_logged_in = $this->session->userdata('logged_in');
        if(!isset($is_logged_in) || $is_logged_in != true)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    function isTeacher(){
        $isTeacher = $this->session->userdata('isTeacher');
        if($isTeacher == '' || $isTeacher != 'isTeacher')
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    function isAdmin(){
        $isAdmin = $this->session->userdata('isAdmin');
        if(!isset($isAdmin) || $isAdmin != 'isAdmin')
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    function index($offset = 0)
    {
        // offset
        $uri_segment = 3;
        $offset = $this->uri->segment($uri_segment);

        // load data
        $khoahocs = $this->Khoahoc_model->get_paged_list($this->limit, $offset)->result();

        // generate pagination
        $this->load->library('pagination');
        $config['base_url'] = site_url('admin/khoahoc/index/');
        $config['total_rows'] = $this->Khoahoc_model->count_all();
        $config['per_page'] = $this->limit;
        $config['uri_segment'] = $uri_segment;
        $this->pagination->initialize($config);
        $this->data['pagination'] = $this->pagination->create_links();

        // generate table data
        $this->load->library('table');
        $this->table->set_empty("&nbsp;");
        $this->table->set_heading('Số thứ tự', 'Id Ngành','Id Môn', 'Tên Khóa Học','Nội dung','Thời lượng', 'Ngày đăng','Trạng thái', 'Quản lý');
        $i = 0 + $offset;
        foreach ($khoahocs as $khoahoc)
        {
            $this->table->add_row(++$i,$khoahoc->id_nganh,$khoahoc->id_mon, $khoahoc->ten_khoahoc,$khoahoc->noidung,$khoahoc->thoi_luong,date('d-m-Y',strtotime($khoahoc->ngaydang)),$khoahoc->state,
                anchor('admin/khoahoc/view/'.$khoahoc->id,'view',array('class'=>'view')).' '.
                    anchor('admin/khoahoc/update/'.$khoahoc->id,'update',array('class'=>'update')).' '.
                    anchor('admin/khoahoc/delete/'.$khoahoc->id,'delete',array('class'=>'delete','onclick'=>"return confirm('Xóa Khóa học Này?')"))
            //anchor('khoahoc/delete/'.$khoahoc->id,'delete',array('class'=>'delete','onclick'=>"return confirm('Are you sure want to delete this khoahoc?')"))
            );
            //coi loi 1
        }
        $this->data['table'] = $this->table->generate();

        // load view
        $this->load->view('admin/khoahoc/khoahocList', $this->data);
    }

    function add()
    {
        // set empty default form field values
        $this->_set_fields();
        // set validation properties
        $this->_set_rules();

        // set common properties
        $this->data['title'] = 'Add new course';
        $this->data['message'] = '';
        $this->data['action'] = site_url('admin/khoahoc/addkhoahoc');
        $this->data['link_back'] = anchor('admin/khoahoc/index/','Back to list of course',array('class'=>'back'));

        // load view
        $this->load->view('admin/khoahoc/khoahocEdit', $this->data);
    }

    public function addkhoahoc()
    {
        // set common properties
        $this->data['title'] = 'Add new couse';
        $this->data['action'] = site_url('admin/khoahoc/addkhoahoc');
        $this->data['link_back'] = anchor('admin/khoahoc/index/','Back to list of courses',array('class'=>'back'));

        // set empty default form field values
        $this->_set_fields();
        // set validation properties
        $this->_set_rules();

        // run validation
        if ($this->form_validation->run() == FALSE)
        {
            $this->data['message'] = '';
        }
        else
        {
            // save data
            $khoahoc = array(
                'id_nganh'=>$this->input->post('id_nganh'),
                'id_mon'=>$this->input->post('id_mon'),
                'ten_khoahoc' => $this->input->post('ten_khoahoc'),

                'noidung'=>$this->input->post('noidung'),
                'thoi_luong'=>$this->input->post('thoi_luong'),
                'ngaydang' => date('Y-m-d', strtotime($this->input->post('ngaydang'))),
                'state'=>$this->input->post('state')
            );
            $id = $this->Khoahoc_model->save($khoahoc);

            // set user message
            $this->data['message'] = '<div class="success">add new cousers success</div>';
        }

        // load view
        $this->load->view('admin/khoahoc/khoahocEdit', $this->data);
    }

    function view($id)
    {
        // set common properties
        $this->data['title'] = 'Cousers Details';
        $this->data['link_back'] = anchor('admin/khoahoc/index/','Back to list of cousers',array('class'=>'back'));

        // get khoahoc details
        $this->data['khoahoc'] = $this->Khoahoc_model->get_by_id($id)->row();

        // load view
        $this->load->view('admin/khoahoc/khoahocView', $this->data);
    }

    function update($id)
    {
        if($this->isAdmin()||$this->isTeacher()){
            // set validation properties
            $this->_set_rules();

            // prefill form values
            $khoahoc = $this->Khoahoc_model->get_by_id($id)->row();
            $this->form_data->id = $id;
            $this->form_data->id_nganh = $khoahoc->id_nganh;
            $this->form_data->id_mon = $khoahoc->id_mon;
            $this->form_data->ten_khoahoc = $khoahoc->ten_khoahoc;
            //$noidung = $this->input->post('noidung');
            $this->form_data->noidung=$khoahoc->noidung;
            $this->form_data->thoi_luong=$khoahoc->thoi_luong;
            $this->form_data->ngaydang = date('d-m-Y',strtotime($khoahoc->ngaydang));
            $this->form_data->state=$khoahoc->state;

            // set common properties
            $this->data['title'] = 'Update Courses';
            $this->data['message'] = '';
            $this->data['action'] = site_url('admin/khoahoc/updatekhoahoc');
            $this->data['link_back'] = anchor('admin/khoahoc/index/','Back to list of courses',array('class'=>'back'));

            // load view
            $this->load->view('admin/khoahoc/khoahocEdit', $this->data);
        }else{
           echo "Ban khong co quyen";
        }
    }

    function updatekhoahoc()
    {
        if($this->isAdmin()||$this->isTeacher()){
            // set common properties
            $this->data['title'] = 'Update Courser';
            $this->data['action'] = site_url('admin/khoahoc/updatekhoahoc');
            $this->data['link_back'] = anchor('admin/khoahoc/index/','Back to list of coures',array('class'=>'back'));

            // set empty default form field values
            $this->_set_fields();
            // set validation properties
            $this->_set_rules();

            // run validation
            if ($this->form_validation->run() == FALSE)
            {
                $this->data['message'] = '';
            }
            else
            {
                // save data
                $id = $this->input->post('id');
                $khoahoc = array(
                    'id_nganh'=>$this->input->post('id_nganh'),
                    'id_mon'=>$this->input->post('id_mon'),
                    'ten_khoahoc' => $this->input->post('ten_khoahoc'),

                    'noidung'=>$this->input->post('noidung'),
                    'thoi_luong'=>$this->input->post('thoi_luong'),
                    'ngaydang' => date('Y-m-d', strtotime($this->input->post('ngaydang'))),
                    'state'=>$this->input->post('state')
                );
                $this->Khoahoc_model->update($id,$khoahoc);

                // set user message
                $this->data['message'] = '<div class="success">Update course success</div>';
            }

            // load view
            $this->load->view('admin/khoahoc/khoahocEdit', $this->data);
        }else{
                echo "Ban khong co quyen";
            }
 }

    function delete($id)
    {
        // delete khoahoc
        $this->Khoahoc_model->delete($id);
        // redirect to khoahoc list page
        redirect('admin/khoahoc/index/','refresh');
    }

    // set empty default form field values
    function _set_fields()
    {
        $this->form_data->id = '';
        $this->form_data->id_nganh = '';
        $this->form_data->id_mon = '';
        $this->form_data->ten_khoahoc = '';

        $this->form_data->noidung = '';
        $this->form_data->thoi_luong = '';
        $this->form_data->ngaydang = '';
        //$this->form_data->state = '';
    }

    // validation rules
    function _set_rules()
    {
        $this->form_validation->set_rules('ten_khoahoc', 'ten_khoahoc', 'trim|required');
        $this->form_validation->set_rules('noidung','NoiDung','trim|required');
        $this->form_validation->set_rules('thoi_luong','ThoiLuong','trim|required');
        $this->form_validation->set_rules('ngaydang','Ngay Dang','trim|required');
        //$this->form_validation->set_rules('state','State','trim|required');

        $this->form_validation->set_message('required', '* Vui lòng điền thông tin vào');
        $this->form_validation->set_message('isset', '* required');
        $this->form_validation->set_message('valid_date', 'date format is not valid. dd-mm-yyyy');
        $this->form_validation->set_error_delimiters('<p class="error">', '</p>');
    }

    // date_validation callback
    function valid_date($str)
    {
        //match the format of the date
        if (preg_match ("/^([0-9]{2})-([0-9]{2})-([0-9]{4})$/", $str, $parts))
        {
            //check weather the date is valid of not
            if(checkdate($parts[2],$parts[1],$parts[3]))
                return true;
            else
                return false;
        }
        else
            return false;
    }
}
?>